Page({
  data: {
    data: null,
    radius: false,
    src: '',
    result: {}
  },
  async onLoad(query) {
    const {
      img
    } = query;
    if (img) {
      this.imgUplode(img);
    }
  },
  handleTap() {
    let that = this;
    my.chooseImage({
      sourceType: ['camera', 'album'],
      count: 1,
      success: (res1) => {
        that.imgUplode(res1.apFilePaths[0]);
      },
    })
  },
  async imgUplode(imagePath) {
    let that = this;
    this.setData({
      src: imagePath
    })
    my.showLoading();
    const res = await my.functionContext.uploadFile({
      //如果是覆盖的同一个图片，下载的时候有缓存，不会立即变化
      cloudPath: 'example' + Date.now() + '.png',
      filePath: imagePath
    });
    const tempUrlRsp = await my.functionContext.getTempFileURL({
      fileList: [{
        fileID: res.fileID,
        maxAge: 60 * 60
      }]
    });
    if (tempUrlRsp.fileList[0].status === 0) {
      my.functionContext.callFunction({
        name: 'getGarbageImgInfo',
        data: {
          imageUrl: tempUrlRsp.fileList[0].tempFileURL,
        },
        success: function (res) {
          my.hideLoading();
          const obj = JSON.parse(res.result.resultStr);
          that.setData({
            result: obj.object[0]
          })
        },
        fail: function (error) {
          my.showToast({
            content: error.errorMessage || '识别失败请重试~'
          })
          my.hideLoading();
          that.setData({
            result: {}
          })
        }
      })
    }
  },
})